package com.example.a2032.activity;

import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.a2032.R;
import com.example.a2032.Utils.SharedPreferenceUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VoiceSettingActivity extends BaseActivity {

    @BindView(R.id.iv_title)
    ImageView ivTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindViews({R.id.iv_range0,R.id.iv_range1,R.id.iv_range2})
    List<ImageView> listViews;
    private int voiceLevel;
    private AudioManager audioManager;
    private int maxVoiceValue;
    private Ringtone ringtone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_voice_setting);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        audioManager= (AudioManager) getSystemService(AUDIO_SERVICE);
         maxVoiceValue =audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
        String title = getIntent().getStringExtra(SettingActivity.KEY_SettingTitle);
        tvTitle.setText(title);
        voiceLevel = SharedPreferenceUtils.getInteger(SharedPreferenceUtils.KEY_VoiceLevel,2);
        showVoiceLevel(voiceLevel);
    }

    @OnClick(R.id.iv_title)
    public void onViewClicked() {
        finish();
    }

    @OnClick({R.id.iv_title, R.id.iv_setting_reduce, R.id.iv_setting_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title:
                finish();
                break;
            case R.id.iv_setting_reduce:
                reduce();
                break;
            case R.id.iv_setting_add:
                add();
                break;
        }
    }
    private void showVoiceLevel(int brightLevel){
        for(ImageView imageView:listViews){
            imageView.setImageResource(R.mipmap.setting_range_unchecked);
        }
        for(int i=0;i<brightLevel+1;i++){
            listViews.get(i).setImageResource(R.mipmap.setting_range_checked);
        }

    }
    private void add(){
        if(voiceLevel <2) voiceLevel++;
        changeVoiceLevel(voiceLevel);
    }
    private void reduce(){
        if(voiceLevel >0) voiceLevel--;
        changeVoiceLevel(voiceLevel);
    }

    private static final String TAG = "VoiceSettingActivity";
    private void changeVoiceLevel(int voiceLevel){
        showVoiceLevel(voiceLevel);
        int setVoiceValue=maxVoiceValue*(voiceLevel+1)/3;
        audioManager.setStreamVolume(AudioManager.STREAM_RING,setVoiceValue,AudioManager.FLAG_PLAY_SOUND);
        playRing();
        SharedPreferenceUtils.setSpInteger(SharedPreferenceUtils.KEY_VoiceLevel, voiceLevel);
    }

    void playRing(){
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        if(ringtone==null){
            ringtone = RingtoneManager.getRingtone(this, notification);
            ringtone.play();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(ringtone!=null)ringtone.stop();
    }
}